import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * EBook Organizer
 * 4/30/2010
 * <P>
 * Interacts between the <code>OrganizerModel</code> and its views.
 * </P>
 * @version 0.1
 */
public class OrganizerController {
	private String versionStr = "v.0.4.1";
	
	private OrganizerModel model;
	private OrganizerView view;
	private JFileChooser fileChooser = new JFileChooser();
	private JFileChooser folderChooser = new JFileChooser();
	
	/**
	 * Different Views
	 */
	private SettingsView settingsView;
	private SettingsController settingsController;
	
	public OrganizerController() {
		fileChooser.setCurrentDirectory(new java.io.File("."));
		
		folderChooser.setCurrentDirectory(new java.io.File("."));
		folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		folderChooser.setAcceptAllFileFilterUsed(false);
	}
	
	private class OpenListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			Object obj = view.getBookList().getSelectedValue();

			System.out.println(model);
			
			if (obj instanceof EBook) {
				EBook book  = (EBook)obj;
			
				if (book != null) {
					
					try {
						String openString = book.toOpenString();
						System.out.println("Openeing : " + openString);
						Runtime.getRuntime().exec( openString );
					}
					catch(Exception e) {
						System.out.println(e.toString());
		                e.printStackTrace();
					}
				}
			}
			else if (obj instanceof Folder) {
				JOptionPane.showMessageDialog(null, "This is a folder");
			}
			else
				JOptionPane.showMessageDialog(null, "Nothing Selected");
			
		}
	}
	
	private class ClearFoldersListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			model.clearFolders();
			
		}
	}
	
	private class RemoveSelectedFolderListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			model.removeFolder( (Folder)view.getFolderList().getSelectedValue() );
		}
	}
	
	private class ReLoadListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			File file = new File( model.getSettingsModel().getFilePath() );
			
			if (file != null) {
				model.load(file);
			}
			else
				JOptionPane.showMessageDialog(null, "No default save file");

		}
	}
	
	private class LoadListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (fileChooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				model.load(file);
			}
			
		}
	}
	
	private class SaveListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
	
			File file = new File(model.getSettingsModel().getFilePath() );
			model.save(file);		
		}
	}
	
	private class SaveAsListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			if (fileChooser.showSaveDialog( null ) == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				model.save(file);
			}
				
		}
	}
	
	private class CloseListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
			
		}
	}

	private class AddFolderListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			if (folderChooser.showOpenDialog( view ) == JFileChooser.APPROVE_OPTION) {
				File file = folderChooser.getSelectedFile();

				Folder folder = new Folder(file, model);
					
				model.addFolder(folder);
			}
				
		}
	}
	
	private class OpenSettingsListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
				settingsController = new SettingsController()	;
				settingsController.setModel( model.getSettingsModel() );
				
				settingsView = new SettingsView( model.getSettingsModel() );
				settingsView.pack();
				settingsView.setLocationRelativeTo( null );
				settingsView.setVisible(true);
				
				settingsController.setSettingsView( settingsView );
		}
	}
	
	private class OpenAboutListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String msg = "Ebook Organizer "+ versionStr + " \n" +
						 "Author: David Rice";	
			JOptionPane.showMessageDialog(null, msg, "About", JOptionPane.PLAIN_MESSAGE);
		}
	}
	
	private class SetCurrentPageListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			EBook book = (EBook)view.getBookList().getSelectedValue();
			int page = Integer.parseInt( view.getAttributesPanel().getCurrentPageTextField().getText() );
			if (book != null)
				book.getAttributes().setCurrentPage(page);
			
		}
		
	}
	
	private class AddFavoriteBookListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			EBook book = (EBook)view.getBookList().getSelectedValue();
			
			if (book!=null) {
				System.out.println("AddFavoriteBookListener");
				view.getFavoritesTree().addBook(book);
			}
		}
	}
	
	private class AddFavoriteFolderListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			Folder folder = (Folder)view.getFolderList().getSelectedValue();
			
			if (folder!=null) {
				System.out.println("AddFavoriteFolderListener");
				view.getFavoritesTree().addFolder(folder);
			}
		}
	}
	
	public void setModel( OrganizerModel m) {
		model = m;
	}
	
	public OrganizerModel getModel() {
		return model;
	}
	
	public void setMainView(OrganizerView v) {
		view = v;
		
		ActionListener openListener = new OpenListener();
		view.getOpenButton().addActionListener( openListener );
		
		ActionListener reLoadListener = new ReLoadListener();
		view.getReloadMenuItem().addActionListener( reLoadListener );
		
		ActionListener loadListener = new LoadListener();
		view.getLoadMenuItem().addActionListener( loadListener );
		
		ActionListener saveListener = new SaveListener();
		view.getSaveMenuItem().addActionListener( saveListener );
		
		ActionListener saveAsListener = new SaveAsListener();
		view.getSaveAsMenuItem().addActionListener( saveAsListener );
		
		ActionListener closeListener = new CloseListener();
		view.getCloseMenuItem().addActionListener( closeListener );
		
		ActionListener addFolderListener = new AddFolderListener();
		view.getAddFolderMenuItem().addActionListener( addFolderListener );
		
		ActionListener clearFoldersListener = new ClearFoldersListener();
		view.getClearFoldersButton().addActionListener( clearFoldersListener );
		
		ActionListener removeSelectedFolderListener = new RemoveSelectedFolderListener();
		view.getRemoveFolderButton().addActionListener( removeSelectedFolderListener );
		
		ActionListener openSettingsListener = new OpenSettingsListener();
		view.getSettingsMenuItem().addActionListener(openSettingsListener);
		
		ActionListener openAboutListener = new OpenAboutListener();
		view.getAboutMenuItem().addActionListener( openAboutListener );
		
		ActionListener setCurrentPageListener = new SetCurrentPageListener();
		view.getAttributesPanel().getCurrentPageTextField().addActionListener( setCurrentPageListener );
		
		
		/**
		 * Favorites
		 */
		ActionListener addFavoriteBookListener = new AddFavoriteBookListener();
		view.getAddFavoriteBookButton().addActionListener( addFavoriteBookListener);
		
		ActionListener addFavoriteFolderListener = new AddFavoriteFolderListener();
		view.getAddFavoriteFolderButton().addActionListener( addFavoriteFolderListener);
		
	}
	
	public OrganizerView getMainView() {
		return view;
	}
}
